$(function () {
    var markers = [];
    var mapObj = new AMap.Map('container', {
        zoom:15,//级别
        // center: [116.397428, 39.90923],//中心点坐标
        viewMode:'3D'//使用3D视图
    });

    var clickHandler = function(e) {
        // alert('您在[ '+e.lnglat.getLng()+','+e.lnglat.getLat()+' ]的位置点击了地图！');
        layer.confirm('是否添加地址?',{
            btn:['确定','取消']
        },function(index){
            //do something
            var locations = '['+e.lnglat.getLng()+','+e.lnglat.getLat()+']';
            console.log(locations+"确定");
            var reMarker = new AMap.Marker({
                position:JSON.parse(locations),   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
                title: '添加',
                icon:'/resources/lib/map/image/ranqiguandao.png'
            });
            reMarker.setMap(mapObj);
            $('#indexs').val(locations);
            reMarker.on("rightclick",makerHandle);
            markers.push(reMarker);
            layer.close(index);
        },function(index){
            //do something
            console.log(index+"取消");

            layer.close(index);
        });
    };
// 绑定事件
    mapObj.on('click', clickHandler);
    //ajax请求
    $.ajax({
        url:'/watchadd/addlist',
        type:'get',
        // data:params,
        cache:false,
        success:function (res) {
            // console.log(JSON.stringify(res));
            for(var i=0;i<res.length;i++){
                var reMarker = new AMap.Marker({
                    position:JSON.parse(res[i].indexs),   // 经纬度对象，也可以是经纬度构成的一维数组[116.39, 39.9]
                    title: res[i].addName,
                    icon:'/resources/lib/map/image/g-loutianranqiguandao.png'
                });
                markers.push(reMarker);
                reMarker.setMap(mapObj);
                reMarker.on("click",makerHandle);
            }
            mapObj.setZoomAndCenter(13,JSON.parse(res[0].indexs));

        },
        error:function (res) {
        }
    });
    function  makerHandle(e) {
        layer.confirm('是否移除地址?',{
            btn:['确定','取消']
        },function (index) {
            var locations = '['+e.lnglat.getLng()+','+e.lnglat.getLat()+']';
            console.log(locations);
             // this.hide();
            // e.hide();
            layer.close(index);
        },function (index) {
            layer.close(index);
        });



        console.log(e);
    }
    mapObj.plugin('AMap.Geolocation', function() {
        var geolocation = new AMap.Geolocation({
            // 是否使用高精度定位，默认：true
            enableHighAccuracy: true,
            // 设置定位超时时间，默认：无穷大
            timeout: 10000,
            // 定位按钮的停靠位置的偏移量，默认：Pixel(10, 20)
            buttonOffset: new AMap.Pixel(10, 20),
            //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见，默认：false
            zoomToAccuracy: true,
            //  定位按钮的排放位置,  RB表示右下
            buttonPosition: 'RB'
        })

        geolocation.getCurrentPosition()
        AMap.event.addListener(geolocation, 'complete', onComplete)
        AMap.event.addListener(geolocation, 'error', onError)

        function onComplete (data) {
            console.log(JSON.stringify(data));
        }

        function onError (data) {
            // 定位出错
            console.log(JSON.stringify(data));
        }
    });



});